﻿using BasicData.Domain;
using BasicData.ErrorCode;
using BasicData.Infastruucture;
using BasicData.Write.API.application.Command.User;
using MediatR;

namespace BasicData.Write.API.application.CommandHandler.User
{
    public class UserStatusCommandHandler : IRequestHandler<UserStatusCommand, ApiResult<object>>
    {
        private readonly IBaseRepository<UserModel> useRepo;

        public UserStatusCommandHandler(IBaseRepository<UserModel> useRepo)
        {
            this.useRepo = useRepo;
        }
        public Task<ApiResult<object>> Handle(UserStatusCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> res = new ApiResult<object>();
            UserModel user = useRepo.GetByModel(request.UserId);
            user.Status = !user.Status;
            int count = useRepo.Update(user);
            if (count > 0)
            {
                res.Code = BaseEnums.Success;
                res.Msg = "用户状态修改成功";
                res.Data = user;
            }
            else
            {
                res.Code = BaseEnums.Failed;
                res.Msg = "用户状态修改失败";
                res.Data = user;
            }
            return Task.FromResult(res);
        }
    }
}
